package com.wzdigit.wms.raw.controller.asn;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.raw.client.request.asn.receive.AddAsnDtlGoodsReceiveRequest;
import com.wzdigit.wms.raw.client.request.asn.receive.ConfirmAsnDtlGoodsReceiveRequest;
import com.wzdigit.wms.raw.client.request.asn.receive.RemoveAsnDtlGoodsReceiveRequest;
import com.wzdigit.wms.raw.client.request.asn.receive.SearchAsnDtlGoodsReceiveRequest;
import com.wzdigit.wms.raw.client.request.asn.receive.SearchAsnDtlReceiveRequest;
import com.wzdigit.wms.raw.client.request.asn.receive.SearchAsnHeaderReceiveRequest;
import com.wzdigit.wms.raw.client.request.asn.receive.UpdateAsnDtlWhsRequest;
import com.wzdigit.wms.raw.client.response.asn.receive.AsnDtlGoodsReceiveResponse;
import com.wzdigit.wms.raw.client.response.asn.receive.AsnDtlReceiveResponse;
import com.wzdigit.wms.raw.client.response.asn.receive.AsnHeaderReceiveResponse;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.asn.AsnReceiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 领料入库pda
 * @author Administrator
 *
 */
@RestController
@Api(tags = "领料入库pda")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class AsnReceiveController {
	
	public static final String REQUEST_MODEL = "/asnreceivegoods";

    @Autowired
    private AsnReceiveService asnReceiveService;
    
    @ApiOperation(value = "主档", notes = "主档")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/searchAsnHeaderReceive")
    public PagerInfo<List<AsnHeaderReceiveResponse>> searchAsnHeaderReceive(
    		SearchAsnHeaderReceiveRequest search, 
    		Pager<?> pager,
            HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
    	try {
            return asnReceiveService.searchAsnHeaderReceive(search, pager, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "明细", notes = "明细")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/searchAsnDtlReceive")
    public List<AsnDtlReceiveResponse> searchAsnDtlReceive(
    		SearchAsnDtlReceiveRequest dto, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
            return asnReceiveService.searchAsnDtlReceive(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "明细货品清单", notes = "明细货品清单")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/searchAsnDtlGoodsReceive")
    public List<AsnDtlGoodsReceiveResponse> searchAsnDtlGoodsReceive(
    		SearchAsnDtlGoodsReceiveRequest dto, 
    		HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
    	try {
            return asnReceiveService.searchAsnDtlGoodsReceive(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "更新收货单物料仓库", notes = "更新收货单物料仓库")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/updateAsnDtlWhs")
    public Result<?> updateAsnDtlWhs(
    		@RequestBody @Valid UpdateAsnDtlWhsRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
    		ValidatorUtil.validator(bindingResult);
            return asnReceiveService.updateAsnDtlWhs(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "清除明细货品清单", notes = "清除明细货品清单")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/removeAsnDtlGoodsReceive")
    public Result<?> removeAsnDtlGoodsReceive(
    		@RequestBody @Valid RemoveAsnDtlGoodsReceiveRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
    		ValidatorUtil.validator(bindingResult);
            return asnReceiveService.removeAsnDtlGoodsReceive(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "扫描预收入库", notes = "扫描预收入库")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/addAsnDtlGoodsReceive")
    public Result<String> addAsnDtlGoodsReceive(
    		@RequestBody @Valid AddAsnDtlGoodsReceiveRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) {
    	try {
    		ValidatorUtil.validator(bindingResult);
            return asnReceiveService.addAsnDtlGoodsReceive(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "确认·", notes = "确认收货")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/confirmAsnDtlGoodsReceive")
    public Result<?> confirmAsnDtlGoodsReceive(
    		@RequestBody @Valid ConfirmAsnDtlGoodsReceiveRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) throws Exception {
    	try {
    		ValidatorUtil.validator(bindingResult);
            return asnReceiveService.confirmAsnDtlGoodsReceive(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}